﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Subsidiary.CouponWrapper
{
    public class YiHaoDianCouponWrapper : ICouponWrapper
    {

        public static readonly string trPattern = @"<tr>(.+?)</tr>";
        public static readonly string numberPattern = @"<td>\s(.+?)\s</td>";
        public static readonly string limitPattern = @"<div style=""position:relative"">(.+?)<div";
        public static readonly string amountPattern = @"<td align=""center"">满(\d+)元抵(\d+)元</td>";
        public static readonly string endDatePattern = @"<td align=""center"">\s(.*)至(.*)</td>";
        public List<CouponItem> PickUp(string html)
        {
            List<CouponItem> coupons = new List<CouponItem>();
            MatchCollection trMatches = Regex.Matches(html, trPattern);
            for (int i = 0; i < trMatches.Count; i++)
            {
                Match trMatch = trMatches[i];
                string trHtml = trMatch.Groups[1].Value;
                Match numberMatch = Regex.Match(trHtml, numberPattern);
                Match limitMatch = Regex.Match(trHtml, limitPattern);
                Match amoutMatch = Regex.Match(trHtml, amountPattern);
                Match endDateMatch = Regex.Match(trHtml, endDatePattern);
                CouponItem coupon = new CouponItem
                {
                    Title = "1号店礼券",
                    Shop = "1号店",
                    Link = "http://www.1haodian.com/",
                    EndDate = endDateMatch.Groups[1].Value.Trim() + " 至 " + endDateMatch.Groups[2].Value.Trim(),
                    Number = numberMatch.Groups[1].Value.Trim(),
                    Password = string.Empty,
                    Amount = float.Parse(amoutMatch.Groups[2].Value.Trim()),
                    Comment = string.Format("买满{0}元可以使用，{1}", amoutMatch.Groups[1].Value.Trim(), limitMatch.Groups[1].Value.Trim())
                };
                coupons.Add(coupon);
            }
            return coupons;
        }
    }
}
